package com.qf.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.business.dao.ICourseDao;
import com.qf.business.entity.Course;
import com.qf.business.entity.CourseSelector;
import com.qf.business.service.CourseSelectorService;
import com.qf.business.service.ICourseSevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * description:
 * author: Ken
 * 公众号：Java架构栈
 */
@Service
@Primary
public class CourseServiceImpl2 extends ServiceImpl<ICourseDao, Course> implements ICourseSevice {

    @Autowired
    private CourseSelectorService courseSelectorService;

    @Autowired
    private ICourseDao courseDao;

    private static Map<Integer, AtomicInteger> courseNum = new ConcurrentHashMap<>();

    private static List<CourseSelector> courseSelectors = new CopyOnWriteArrayList<>();

    /**
     * 选课的业务
     * @param cid
     * @param sid
     * @return
     */
    @Override
    @Transactional
    public int selectCourse(Integer cid, Integer sid) {

        //判断当前是否已经选过这门课
        Long count = courseSelectorService.query()
                .eq("cid", cid)
                .eq("sid", sid)
                .count();

        if (count > 0) {
            //已经存在
            return -2;
        }

        //通过课程id 查询在内存中是否拥有当前的课程信息
        AtomicInteger nums = courseNum.get(cid);
        if (nums == null) {
            synchronized ((cid + "").intern()) {
                nums = courseNum.get(cid);
                if (nums == null) {
                    //当前内存中没有这个信息
                    Course course = super.getById(cid);
                    //放入Map中
                    nums = new AtomicInteger(course.getHasNumber());
                    courseNum.put(cid, nums);
                }
            }
        }
        //线程1
        //线程2

        //进行座位数的扣减
        int value = nums.getAndDecrement();
        if (value == 1) {
            System.out.println("---->选课已经完成，同步到数据库中！");
            //将数据同步到数据库中
            Course course = new Course()
                    .setHasNumber(0)
                    .setId(cid);
            course.setCreateTime(null);
            course.setStatus(null);
            courseDao.updateById(course);

            //清楚下内存
            courseNum.remove(cid);
        } else if (value <= 0){
            //座位已经抢完
            return -1;
        }

        //已经抢到该座位
        //添加中间表
        CourseSelector courseSelector = new CourseSelector()
                .setSid(sid)
                .setCid(cid);
        //保存到内存里
//        courseSelectors.add(courseSelector);
        //保存记录
        courseSelectorService.save(courseSelector);

        return 1;
    }
}
